home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_498 / wordsearch / src / file.c next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  208 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/libraries.h>
  4. #include "wsearch.h"
  5. #include "interface.h"
  6. #include <proto/req.h>
  7. #include "funcs.h"
  8.  
  9. char file[DSIZE+FCHARS] = "";
  10.  
  11. struct ReqFileRequester    MyFileReqStruct;
  12. char filename[FCHARS] = "";
  13. char directoryname[DSIZE] = "";
  14.  
  15. BOOL filerequest(file)
  16.     char *file;
  17. {
  18.     BOOL result;
  19.     
  20.     file[0] = 0;
  21.  
  22.         /* Initialize the 'PathName' field so that the file requester will */
  23.         /* construct a complete path name for me.  It will also put the two */
  24.         /* parts of the answer (directory and file name) into the directory */
  25.         /* file name which I also decided to supply it with.  Since the */
  26.         /* directory and file name arrays are present, it will use their */
  27.         /* initial contents as the initial file and directory.  If they aren't */
  28.         /* present it will leave both blank to start. */
  29.     MyFileReqStruct.PathName = file;
  30.     MyFileReqStruct.Dir = directoryname;
  31.     MyFileReqStruct.File = filename;
  32.  
  33.         /* The directory caching of this file requester is one of its nice */
  34.         /* features, so I decided to show it off.  It is completely optional */
  35.         /* though, so if you don't want it, don't set this flag.  If you do */
  36.         /* want it, don't forget to call PurgeFiles() when you are done. */
  37.     MyFileReqStruct.Flags = FRQCACHINGM;
  38.  
  39.         /* Initialize a few colour fields.  Not strictly necessary, but */
  40.         /* personally, I like having my files a different colour from my */
  41.         /* directories. */
  42.     MyFileReqStruct.dirnamescolor = 2;
  43.     MyFileReqStruct.devicenamescolor = 2;
  44.         /* I could also make it larger, pass it a file and/or directory */
  45.         /* name, set the window title, set various flags and customize */
  46.         /* in many other ways, but I wanted to show that it can be easily */
  47.         /* used without having to fill in a lot of fields. */
  48.     result = FileRequester(&MyFileReqStruct);
  49.     PurgeFiles(&MyFileReqStruct);
  50.         
  51.     return(result);
  52. }
  53.  
  54. /* BOOL filerequest(file)
  55.  char *file;
  56.  {
  57.     printf("Enter filename:\n",file);
  58.     scanf("%s",file);
  59.     return(TRUE);
  60.  }
  61.  */
  62.  
  63. BOOL loadfile()
  64. {
  65.     FILE *ptr;
  66.     int i,j;
  67.     int tpx, tpy, tw, trot, tfilter;
  68.     
  69.     tpx = px; tpy=py; tw = w; trot = rot; tfilter = filter;
  70.  
  71.     if(filerequest(file)==FALSE) return(FALSE);
  72.     if(file[0]==0) return(FALSE);
  73.  
  74.     ptr = fopen(file,"r");
  75.     if(ptr<=0)
  76.         return(FALSE);
  77.     else
  78.     {
  79.         fscanf(ptr,"%d %d %d %d %d",&px,&py,&w,&rot,&filter);
  80.     fgets(word[0],MAXSIZE,ptr); /* get rid of eol */
  81.         for(i=0;i<=w;i++)
  82.     {
  83.                 fgets(word[i],MAXSIZE,ptr);
  84.         if(word[i][strlen(word[i])-1]=='\n')
  85.             word[i][strlen(word[i])-1]=0;
  86.     }
  87.     if(AllocDims()==FALSE)
  88.     {
  89.         px = tpx; py = tpy; w = tw; rot = trot; filter = tfilter;
  90.         return(FALSE);
  91.     }
  92.         for(i=0;i<=px;i++)
  93.                 for(j=0;j<=py;j++)
  94.                         fscanf(ptr,"%c%c",&Key(i,j),&Puzzle(i,j));
  95.         fclose(ptr);
  96.  
  97.         if(rot==0) rot=1;
  98.         for(i=0;i<8;i++)
  99.                 M2I2[i].Flags=M2I2[i].Flags&~CHECKED;
  100.         M2I2[rot].Flags=M2I2[i].Flags|CHECKED;
  101.  
  102.         if(filter==0) filter=255;
  103.         for(i=0;i<8;i++)
  104.                 if((filter&1<<i)!=0)
  105.                         M1I2[i].Flags=M1I2[i].Flags|CHECKED;
  106.                 else
  107.                         M1I2[i].Flags=M1I2[i].Flags&~CHECKED;
  108.  
  109.         return(TRUE);
  110.     }
  111. }
  112.  
  113. BOOL savefile()
  114. {
  115.     FILE *ptr;
  116.     int i,j;
  117.  
  118.     if(file[0]==0)
  119.         if(saveasfile()==FALSE) return(FALSE);
  120.     else
  121.     {
  122.         ptr = fopen(file,"w");
  123.         if(ptr<=0)
  124.                 return(FALSE);
  125.         else
  126.         {
  127.                 fprintf(ptr,"%d %d %d %d %d\n",px,py,w,rot,filter);
  128.                 for(i=0;i<=w;i++)
  129.                         fprintf(ptr,"%s\n",word[i]);
  130.                 for(i=0;i<=px;i++)
  131.                         for(j=0;j<=py;j++)
  132.                                 fprintf(ptr,"%c%c",Key(i,j),Puzzle(i,j));
  133.                 fclose(ptr);
  134.                 return(TRUE);
  135.         }
  136.     }
  137. }
  138.  
  139. BOOL saveasfile()
  140. {
  141.         if(filerequest(file)==FALSE)
  142.                 return(FALSE);
  143.         else if(file[0]!=0)
  144.                 return(savefile());
  145. }
  146.  
  147.  
  148. void printlist()
  149. {
  150.     FILE *ptr;
  151.     int i,w;
  152.     static char file[100]="";
  153.  
  154.     if((M0I5[0].Flags&CHECKED)!=0)
  155.     {
  156.         if(filerequest(file)==FALSE) return;
  157.     }
  158.     else
  159.     {
  160.         strcpy(file,"PRT:");
  161.     }
  162.  
  163.     ptr = fopen(file,"w");
  164.     if( ptr>0 )
  165.      {
  166.         w=MAXWORD-1;
  167.         while(word[w][0]==0 && w!=-1)
  168.             w--;
  169.         i = 0;
  170.         while(i<=w)
  171.         {
  172.             fprintf(ptr,"%s\n",word[i]);
  173.             i++;
  174.         }
  175.         fclose(ptr);
  176.      }
  177. }
  178.  
  179. void printpuz()
  180. {
  181.     FILE *ptr;
  182.     int i,w;
  183.     static char file[100]="";
  184.  
  185.     if((M0I5[0].Flags&CHECKED)!=0)
  186.     {
  187.         if(filerequest(file)==FALSE) return;;
  188.     }
  189.     else
  190.     {
  191.         strcpy(file,"PRT:");
  192.     }
  193.  
  194.     ptr = fopen(file,"w");
  195.     if( ptr>0 )
  196.      {
  197.         if(rot<5) w=py; else w = py;
  198.         i = 0;
  199.         while(i<=w)
  200.         {
  201.             fprintf(ptr,"%s\n",&Display(i,0));
  202.             i++;
  203.         }
  204.         fclose(ptr);
  205.      }
  206. }
  207.  
  208.